﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DNNToolkit.Modules.FAQ.Components;
using DotNetNuke.Common.Utilities;

namespace DNNToolkit.Modules.FAQ.Components
{
    public class DataManager
    {
        private FAQDataContext faqDC = new FAQDataContext(Config.GetConnectionString());
        
        /// -----------------------------------------------------------------------------
        /// <summary>
        /// adds an object to the database.
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="objFAQ">The FAQInfo object.</param>
        /// <history>
        /// </history>
        /// -----------------------------------------------------------------------------
        public void CreateFAQ(FAQInfo objFAQ)
        {
            //FAQInfo faq = new FAQInfo();
            //faq.Question = question;
            //faq.QuestionRichText = questionRichText;
            //faq.Answer = answer;
            //faq.AnswerRichText = answerRichText;
            //faq.ModuleID = moduleID;
            //faq.DateTimeStamp = dateTimeStamp;
            faqDC.FAQInfo.InsertOnSubmit(objFAQ);
            faqDC.SubmitChanges();
        }

        /// <summary>
        /// Deletes the FAQ.
        /// </summary>
        /// <param name="objFAQ">The obj FAQ.</param>
        public void DeleteFAQ(FAQInfo objFAQ)
        {
            var faq = faqDC.FAQInfo.SingleOrDefault(m => m.FAQID == objFAQ.FAQID);
            if (faq != null)
            {
                faqDC.FAQCategoryInfo.DeleteAllOnSubmit(objFAQ.FAQCategoryInfo);
                faqDC.FAQInfo.DeleteOnSubmit(faq);
                faqDC.SubmitChanges();
            }
        }

        /// <summary>
        /// Checks the concurrency exception.
        /// </summary>
        /// <param name="objFAQ">The obj FAQ.</param>
        protected void CheckConcurrencyException(FAQInfo objFAQ)
        {
            FAQInfo faq = GetFAQ(objFAQ.FAQID, objFAQ.ModuleID);
            if (faq == null || faq.DateTimeStamp != objFAQ.DateTimeStamp)
                throw new ArgumentException(Resource.Strings.E_ConcurrencyException);
        }

        /// <summary>
        /// Updates the FAQ.
        /// </summary>
        /// <param name="objFAQ">The obj FAQ.</param>
        public void UpdateFAQ(FAQInfo objFAQ)
        {
            FAQInfo faq = faqDC.FAQInfo.SingleOrDefault(m => m.FAQID == objFAQ.FAQID);
            if (faq != null)
            {
                faq.Question = objFAQ.Question;
                faq.QuestionRichText = objFAQ.QuestionRichText;
                faq.Answer = objFAQ.Answer;
                faq.AnswerRichText = objFAQ.AnswerRichText;
                faq.ModuleID = objFAQ.ModuleID;
                faq.DateTimeStamp = objFAQ.DateTimeStamp;
                faqDC.SubmitChanges();
            }
        }


        /// <summary>
        /// Gets the FAQ.
        /// </summary>
        /// <param name="FAQID">The FAQ ID.</param>
        /// <param name="moduleID">The module ID.</param>
        /// <returns></returns>
        public FAQInfo GetFAQ(long fAQID, int moduleID)
        {
            var faq = faqDC.FAQInfo.SingleOrDefault(m => m.FAQID == fAQID && m.ModuleID == moduleID);

            return faq;
        }

        /// <summary>
        /// Searches the FAQ.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="moduleID">The module ID.</param>
        /// <returns></returns>
        public List<FAQInfo> SearchFAQ(string filter, int moduleID)
        {
            return faqDC.FAQInfo.Where(x => x.Question.ToLower().Contains(filter.ToLower()) || x.Answer.ToLower().Contains(filter.ToLower())).ToList();
        }

        /// <summary>
        /// Searches the FAQ by filter.
        /// </summary>
        /// <param name="filter">The filter.</param>
        /// <param name="category">The category.</param>
        /// <param name="moduleID">The module ID.</param>
        /// <returns></returns>
        public List<FAQInfo> SearchFAQByFilter(string filter, long categoryID, int moduleID)
        {
            var list = SearchFAQByCatetory(categoryID,moduleID);
            return list.FindAll(x => x.Question.ToLower().Contains(filter.ToLower()) || x.Answer.ToLower().Contains(filter.ToLower()));
        }

        /// <summary>
        /// Searches the FAQ by catetory.
        /// </summary>
        /// <param name="category">The category.</param>
        /// <param name="moduleID">The module ID.</param>
        /// <returns></returns>
        public List<FAQInfo> SearchFAQByCatetory(long categoryID, int moduleID)
        {
            var list = faqDC.FAQInfo.Where(w=>w.ModuleID==moduleID).ToList();
            var categoryList = faqDC.FAQCategoryInfo
                .Where(x=>x.CategoryDefinitionID == categoryID);
            return list.FindAll(x=>categoryList.Any(y=>y.FAQID==x.FAQID));
        }


        /// <summary>
        /// Gets all FAQ category definitions.
        /// </summary>
        /// <returns></returns>
        public List<FAQCategoryDefinitionInfo> GetAllFAQCategoryDefinitions()
        {
            return faqDC.FAQCategoryDefinitionInfo.ToList();
        }

        /// <summary>
        /// Gets the FAQ category definition.
        /// </summary>
        /// <param name="CategoryDefinitionID">The category definition ID.</param>
        /// <returns></returns>
        public FAQCategoryDefinitionInfo GetFAQCategoryDefinition(long CategoryDefinitionID)
        {
            return GetAllFAQCategoryDefinitions().Find(x => x.CategoryDefinitionID == CategoryDefinitionID);
        }

        /// <summary>
        /// Creates the FAQ category definition.
        /// </summary>
        /// <param name="objFAQCategoryDefinition">The obj FAQ category definition.</param>
        public void CreateFAQCategoryDefinition(FAQCategoryDefinitionInfo objFAQCategoryDefinition)
        {
            faqDC.FAQCategoryDefinitionInfo.InsertOnSubmit(objFAQCategoryDefinition);
            faqDC.SubmitChanges();
        }


        /// <summary>
        /// Updates the FAQ category definition.
        /// </summary>
        /// <param name="objFAQCategoryDefinition">The obj FAQ category definition.</param>
        public void UpdateFAQCategoryDefinition(FAQCategoryDefinitionInfo objFAQCategoryDefinition)
        {
            var faqCategoryDefinition = faqDC.FAQCategoryDefinitionInfo.SingleOrDefault(m => m.CategoryDefinitionID == objFAQCategoryDefinition.CategoryDefinitionID);
            if (faqCategoryDefinition != null)
            {
                faqCategoryDefinition.CategoryName = objFAQCategoryDefinition.CategoryName;
                faqDC.SubmitChanges();
            }
        }


        /// <summary>
        /// Deletes the FAQ category definition.
        /// </summary>
        /// <param name="objFAQCategoryDefinition">The obj FAQ category definition.</param>
        public void DeleteFAQCategoryDefinition(FAQCategoryDefinitionInfo objFAQCategoryDefinition)
        {
            var faqCategoryDefinition = faqDC.FAQCategoryDefinitionInfo.SingleOrDefault(m => m.CategoryDefinitionID == objFAQCategoryDefinition.CategoryDefinitionID);
            if (faqCategoryDefinition != null)
            {
                faqDC.FAQCategoryInfo.DeleteAllOnSubmit(faqCategoryDefinition.FAQCategoryInfo);
                faqDC.FAQCategoryDefinitionInfo.DeleteOnSubmit(faqCategoryDefinition);
                faqDC.SubmitChanges();
            }
        }

    }
}
